// 常用正则规则大全：https://any86.github.io/any-rule/
export const commRules = {
	phone: { pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/, message: '请填写符合要求的11位手机号', trigger: 'blur' },
	email: { pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: '请填写正确的邮箱号', trigger: 'blur' },
	idCard: {
		pattern: /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0[1-9]|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/,
		message: '请填写符合要求的身份证号',
		trigger: 'blur',
	},
	lettersNum: { pattern: /^[A-Za-z0-9]+$/, message: '填写内容须是字母或数字组成', trigger: 'blur' },
	number: { pattern: /^\d{1,}$/, message: '填写内容必须是纯数字', trigger: 'blur' },
	url: {
		pattern: /^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/,
		message: '填写内容必须是网址',
		trigger: 'blur',
	},
	uscc: { pattern: /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/, message: '请填写符合要求的统一社会信用代码', trigger: 'blur' },
	money: { pattern: /^-?\d+(,\d{3})*(\.\d{1,2})?$/, message: '请填写符合要求的金额', trigger: 'blur' },
	mobile: { pattern: /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/, message: '请填写符合要求的座机号', trigger: 'blur' },
	contact: {
		pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/ | /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/,
		message: '请填写手机号或座机号',
		trigger: 'blur',
	},
	password: {
		pattern:
			/^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]/,
		message: '密码格式为大写字母，小写字母，数字，特殊符号 `@#$%^&*`~()-+=` 中任意3项密码',
		trigger: 'blur',
	},
};
export const required = (message, type = 'string', trigger = ['blur', 'change']) => ({ required: true, message, trigger, type });

export const existRules = (message, type) => ({
	required: false,
	message,
	trigger: ['change'],
	validator(obj, value, rule) {
		const rules = commRules[type];
		if (!rules.pattern.test(value)) {
			return new Error(message || rules.message);
		}
	},
});
